home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Divers / DinkClass ƒ / DC TextEdit / DTEditDoc.c < prev    next >
Encoding:
Text File  |  1992-07-12  |  4.3 KB  |  259 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        DTEditDoc.c
  3.  
  4.     Written by:    Mark Gross
  5.  
  6.     Copyright:    © 1992 by Applied Technical Software, all rights reserved.
  7.     Use at your own risk.
  8.  
  9. */
  10.  
  11. //This is the diffinition of the DTEditDoc-ument Class
  12.  
  13. #include "DTEditDoc.h"
  14. #include "DTEditWind.h"
  15. #include "DApplication.h"
  16.  
  17.  
  18. DTEditDoc::DTEditDoc()
  19. {
  20.     fText != NULL;
  21. }
  22.  
  23. DTEditDoc::~DTEditDoc()
  24. {
  25.     if( fText != NULL)
  26.         TEDispose( fText);
  27. }
  28.  
  29.  
  30. DDocument* DTEditDoc::Init( Boolean OpenFromFile)
  31. {
  32.     Rect    view, dest;
  33.     DDocument *inheritedDoc;
  34.     
  35.     dest.left = 4;
  36.     dest.top = 4;
  37.     dest.right = 400;
  38.     dest.bottom = kMaxShort;
  39.     view.left = 0; view.top = 0; view.right = 0; view.bottom = 0;
  40.     fText = TENew(&dest, &view);
  41.     
  42.     inheritedDoc = inherited::Init(OpenFromFile);
  43.  
  44.      return inheritedDoc;
  45. }
  46.  
  47. void DTEditDoc::AEInitDoc(FSSpec *theFSS)
  48. {
  49.     Rect    view, dest;
  50.     DDocument *inheritedDoc;
  51.     
  52.     dest.left = 4;
  53.     dest.top = 4;
  54.     dest.right = 400;
  55.     dest.bottom = kMaxShort;
  56.     view.left = 0; view.top = 0; view.right = 0; view.bottom = 0;
  57.     fText = TENew(&dest, &view);
  58.     
  59.     inherited::AEInitDoc(theFSS);        
  60. }//end member function AEInitDoc
  61.  
  62.  
  63. void DTEditDoc::HandleMenuChoice(short menuID, short menuItem)
  64. {
  65.     if( menuID == rEditMenu)
  66.     {
  67.         switch (menuItem)
  68.         {
  69.             case iUndo:
  70.                 ;//do nothing, for now
  71.                 break;
  72.                 
  73.             case iCut:
  74.                 DoCut();
  75.                 break;
  76.                 
  77.             case iCopy:
  78.                 DoCopy();
  79.                 break;
  80.                 
  81.             case iPaste:
  82.                 DoPaste();
  83.                 break;
  84.                 
  85.             case iClear:
  86.                 DoClear();
  87.                 break;
  88.                 
  89.             case iSelectAll:
  90.                 DoSellectAll();
  91.                 break;
  92.                 
  93.             default:
  94.                 break;
  95.                 
  96.         }// end switch on menuItem
  97.     }
  98.  
  99.     inherited::HandleMenuChoice(menuID, menuItem);
  100. }// end of HandleMenuChoice member fuction
  101.  
  102.              
  103. void    DTEditDoc::DoSellectAll(void)
  104. {
  105.     TESetSelect(0, kMaxShort, fText);
  106. }
  107.  
  108.  
  109.  void DTEditDoc::SetUpMenues(void)
  110.  {
  111.     MenuHandle    menu;
  112.     
  113.     menu = GetMHandle(rEditMenu);
  114.  
  115.     EnableMenuItem( menu, iUndo, FALSE);
  116.     if ( (**fText).selStart != (**fText).selEnd )
  117.     {
  118.         EnableMenuItem( menu, iCut, TRUE);
  119.         EnableMenuItem( menu, iCopy, TRUE);
  120.         EnableMenuItem( menu, iClear, TRUE);
  121.     }
  122.     else
  123.     {
  124.         EnableMenuItem( menu, iCut, FALSE);
  125.         EnableMenuItem( menu, iCopy, FALSE);
  126.         EnableMenuItem( menu, iClear, FALSE);
  127.     }
  128.     if ( (gApplication->fClipData)&&(gApplication->fClipType == 'TEXT') )
  129.         EnableMenuItem( menu, iPaste, TRUE);
  130.     else
  131.         EnableMenuItem( menu, iPaste, FALSE);
  132.     
  133.     EnableMenuItem( menu, iSelectAll, TRUE);
  134.     
  135.     inherited::SetUpMenues();
  136. }// end of SetUpMenues function
  137.  
  138.  
  139. DWindow* DTEditDoc::MakeWindow(Boolean hasColorWindows)
  140. {    
  141.     DTEditWind *newWindow;
  142.     Rect    view;
  143.         
  144.     newWindow = new DTEditWind;
  145.     fDWindow = newWindow;
  146.         
  147.     if (newWindow->Init(this, hasColorWindows))
  148.     {
  149.         newWindow->GetContentRect( &view);
  150.         (**fText).viewRect = view;
  151.         TEAutoView(TRUE, fText);
  152.         
  153.         // if your application needs a click loop set it here using
  154.         // SetClikLoop(MyClickLoop, fTEHandle)
  155.         
  156.         newWindow->ValidateVertScrollRange();
  157.  
  158.         return newWindow;
  159.     }
  160.     else
  161.     {
  162.         fDWindow = NULL;
  163.         return fDWindow;
  164.     }    
  165.     
  166. }//end member function MakeWindow
  167.  
  168.  
  169. OSErr DTEditDoc::ReadData(short refNum, long *size)
  170. {
  171.     OSErr fileError;
  172.     Handle data;
  173.     
  174.     data = NewHandle(*size);
  175.     if(*size > kMaxShort)
  176.         *size = kMaxShort;
  177.     HLock(data);
  178.     fileError = FSRead(refNum, size, *data);
  179.     
  180.     if(fileError != noErr)
  181.     {
  182.         HUnlock(data);
  183.         TEDispose( fText);
  184.         fText = NULL;
  185.         return fileError;
  186.     }
  187.     
  188.     TESetText(*data, *size, fText);
  189.     TESetSelect( 0, 0, fText);
  190.     HUnlock(data);
  191.     DisposHandle(data);
  192.     
  193.     fFileRef = refNum;
  194.     return fileError;    
  195. }
  196.  
  197.  
  198. OSErr DTEditDoc::WriteData(short refNum)
  199. {
  200.     fDataHandle = (Handle)TEGetText(fText);
  201.     return inherited::WriteData(refNum);
  202. }
  203.  
  204.  
  205.  
  206. void    DTEditDoc::DoCopy(void)
  207. {
  208.     Handle TEData;
  209.  
  210.     TECopy(fText);
  211.     TEData = TEScrapHandle();
  212.     
  213.     gApplication->GiveDataToApp(TEData, 'TEXT');
  214.  
  215. }// end of DoCopy member fuction
  216.  
  217.  
  218.  
  219. void    DTEditDoc::DoClear(void)
  220. {
  221.     TEDelete(fText);
  222.     fNeedToSave = TRUE;
  223.     ((DTEditWind *)fDWindow)->SynchScrollBars();
  224. }// end of DoCopy member fuction
  225.  
  226.  
  227.  
  228. void    DTEditDoc::DoCut(void)
  229. {
  230.     DoCopy();
  231.     DoClear();
  232. }// end of DoCut member fuction
  233.  
  234.  
  235.  
  236. void    DTEditDoc::DoPaste(void)
  237. {
  238.     OSType type;
  239.     Handle tempHandle;
  240.     Handle teScrap;
  241.     long     scrapLen;
  242.     
  243.     tempHandle = gApplication->GetClipCopyFromApp(&type);
  244.     if(type == 'TEXT')
  245.     {
  246.         teScrap = TEScrapHandle( );            
  247.         HandAndHand( tempHandle, teScrap);
  248.         
  249.         scrapLen = GetHandleSize(teScrap);
  250.         TESetScrapLen(scrapLen);
  251.  
  252.         TEPaste(fText);
  253.         fNeedToSave = TRUE;
  254.         ((DTEditWind *)fDWindow)->SynchScrollBars();
  255.     }
  256. }// end of DoPaste member fuction
  257.  
  258.  
  259.